//
//  Fraction.m
//  OC02_类和对象
//
//  Created by Hanlingyu on 16/7/5.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (instancetype)initWithNumerator:(NSInteger)numerator
                      denominator:(NSInteger)denominator {
    self = [super init];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
        
    }
    if (_denominator == 0) {
        NSLog(@"分母不能为0");
    }
    return self;
}

+ (instancetype)fractionWithNumerator:(NSInteger)numerator
                          denominator:(NSInteger)denominator {
    return [[Fraction alloc] initWithNumerator:numerator denominator:denominator];
}

// 求最大公约数的方法:
- (NSInteger)gcdOfFirstNumber:(NSInteger)firstNumber
                 secondNumber:(NSInteger)secondNumber {
    if (firstNumber == 0 || secondNumber == 0) {
        return 0;
    } else {
    while (firstNumber % secondNumber) {
        NSInteger temp = firstNumber;
        firstNumber = secondNumber;
        secondNumber = temp % secondNumber;
    }
        return secondNumber;
    }
}
// 约分方法的实现
- (void)reduction {
    NSInteger gcd = [self gcdOfFirstNumber:self.numerator secondNumber:self.denominator];
    if (gcd != 0) {
        self.numerator /= gcd;
        self.denominator /= gcd;
    }
}

- (Fraction *)addWith:(Fraction *)anotherFraction {
        NSInteger newNumerator = self.numerator * anotherFraction.denominator + anotherFraction.numerator * self.denominator;
        NSInteger newDenominator = self.denominator * anotherFraction.denominator;
        Fraction *result = [[Fraction alloc] initWithNumerator:newNumerator denominator:newDenominator];
        [result reduction];
        return result;
}

- (Fraction *)subWith:(Fraction *)anotherFraction {
    NSInteger newNumerator = self.numerator * anotherFraction.denominator - anotherFraction.numerator * self.denominator;
    NSInteger newDenominator = self.denominator * anotherFraction.denominator;
    Fraction *result = [[Fraction alloc] initWithNumerator:newNumerator denominator:newDenominator];
    [result reduction];
    return result;
}
- (Fraction *)mulWith:(Fraction *)anotherFraction {
    NSInteger newNumerator = self.numerator * anotherFraction.numerator;
    NSInteger newDenominator = self.denominator * anotherFraction.denominator;
    Fraction *result = [[Fraction alloc] initWithNumerator:newNumerator denominator:newDenominator];
    [result reduction];
    return result;
}

- (Fraction *)divWith:(Fraction *)anotherFraction {
    NSInteger newNumerator = self.numerator * anotherFraction.denominator;
    NSInteger newDenominator = anotherFraction.numerator * self.denominator;
    Fraction *result = [[Fraction alloc] initWithNumerator:newNumerator denominator:newDenominator];
    [result reduction];
    return result;
}

- (void)outPut {
    NSLog(@"%ld/%ld", self.numerator, self.denominator);
}

// 重写打印方法
- (NSString *)description {
    return [NSString stringWithFormat:@"<%ld-%ld>", self.numerator, self.denominator];
}

@end
